#ifndef PARTICLE_H_
#define PARTICLE_H_
#include <gl\gl.h>			
#include <gl\glu.h>		
#include <gl\glut.h>
#include "object.h"
class particle{
public:
	particle(){}
	void initU(GLint i)
	{
		Q=MyV(-0.6,0.2*(float)rand()/32767-0.1,0.2*(float)rand()/32767-0.1);
		V=MyV(0.2,Q.y/2,Q.z/2);
		A=MyV(0,0,0);
		lifemax=4;
		life=lifemax;
		idx=i;
	}
	void setAcc(GLfloat amod)
	{
		amod=amod*(float)rand()/32767;
		A=MyV(amod,amod*((float)rand()/32767-0.5),amod*((float)rand()/32767-0.5));
	}
	void move()
	{
		Q=Q+V;
	}
	void speedup()
	{
		V=A;
	}
	void fade()
	{
		life-=fabs(fabs(Q.y)+fabs(Q.z));
		if (life<0)
			initU(idx);
	}
	const GLfloat* getPos(){
		return Q.V();
	}
	const GLubyte* getColor(){
		return C.V();
	}

	Color C;
	GLfloat lifemax;
	GLfloat life;
	MyV A,Q,V;
	GLint idx;
} ;
class ParticleSystem{
public:
	ParticleSystem(GLint _initCnt){
		initCnt=_initCnt;
		cout<<initCnt<<endl;
		for (int i=1;i<=initCnt;i++)
			P[i].initU(i);
		amod=0.2;
		lifemax=10;
	}
	ParticleSystem(){}
	void drawOneFrame()
	{
		glShadeModel(GL_SMOOTH);
	    glEnable(GL_TEXTURE_2D);
	  	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
	    glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE);
		glEnable(GL_BLEND);
		for (int i=1;i<=initCnt;i++)
			{	
				glPushMatrix();
				GLfloat x=P[i].Q.x,y=P[i].Q.y,z=P[i].Q.z;
				GLfloat s=0.15;
				
				glTranslatef(x,y,z);
				glRotatef((float)rand()/32767*180,rand(),rand(),rand());
				glTranslatef(-x,-y,-z);
				glColor4ub(P[i].C.r,P[i].C.g,P[i].C.b,0);
				glBegin(GL_TRIANGLE_STRIP);						
				    glTexCoord2d(1,1); glVertex3f(x+s,y+s,z); 
					glTexCoord2d(0,1); glVertex3f(x-s,y+s,z); 
					glTexCoord2d(1,0); glVertex3f(x+s,y-s,z); 
					glTexCoord2d(0,0); glVertex3f(x-s,y-s,z); 
				glEnd();
				glPopMatrix();
			}
		glDisable(GL_BLEND);

	}
	void PrepareNextFrame()
	{
		for (int i=1;i<=initCnt;i++)
		{
			P[i].move();
			P[i].speedup();
			P[i].setAcc(amod);
			P[i].fade();
		}
	}
private:
	GLfloat amod;
	GLfloat lifemax;
	particle P[10000];
	int initCnt;

} ;
#endif /*PARTICLE_H_*/
